package com.nopay.dxdwphone.ui.ativity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.nopay.dxdwphone.base.BaseActivity;
import com.nopay.dxdwphone.bean.PersonInfoBean;
import com.nopay.dxdwphone.bean.WechatBean;
import com.nopay.dxdwphone.exception.FileNotSupportException;
import com.nopay.dxdwphone.utils.ExcelUtils;
import com.nopay.dxdwphone.utils.FileUtils;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.nopay.dxdwphone.Constants;
import com.nopay.dxdwphone.R;
import com.nopay.dxdwphone.databinding.ActivityImportWechatBinding;

import com.nopay.dxdwphone.ui.adapter.WechatAdapter;

/**
 * @ProjectName: myVisualPhone
 * @Package: cn.cn.myvisualphone.ui.ativity
 * @ClassName: ForgetActivity
 * @Description: java类作用描述
 * @Author: anqi
 * @CreateDate: 2021/5/23 8:05
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/23 8:05
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class FileImportWechatActivity extends BaseActivity<ActivityImportWechatBinding> {

    public static final String FOLDER_WEIXIN = Environment.getExternalStorageDirectory().getPath() + "/Download/WeiXin/";

    private List<WechatBean> list = new ArrayList<>();
    private String filePath;

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(FileImportWechatActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSwipeBackHelper.backward();

            }
        });
        binding.rv.setLayoutManager(new LinearLayoutManager(FileImportWechatActivity.this));
        WechatAdapter wechatAdapter = new WechatAdapter(list);
        binding.rv.setAdapter(wechatAdapter);
        wechatAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                WechatBean item = (WechatBean) adapter.getItem(position);
//                if (view.getId() == R.id.rl) {
//                    ReadExcelTask task = new ReadExcelTask();
//                    task.execute(filePath + item.getFileName());
//
//                }

                Intent intent = new Intent();
                intent.putExtra(Constants.WECHAT_FILE_NAME, item.getFileName());
                setResult(RESULT_OK, intent);
                mSwipeBackHelper.backward();
            }
        });
        requetPermission(1);
    }

    class ReadExcelTask extends AsyncTask<String, Integer, String> {
        private List<PersonInfoBean> infoBeanList;

        @Override
        protected String doInBackground(String... strings) {
            try {
                infoBeanList = ExcelUtils.parseExcel(strings[0]);
            } catch (FileNotSupportException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (infoBeanList != null && !infoBeanList.isEmpty()) {
                Intent intent = new Intent();
                intent.putExtra(Constants.EX_WECHAT, (Serializable) infoBeanList);
                setResult(RESULT_OK, intent);
                mSwipeBackHelper.backward();
            }
        }
    }

    private void requetPermission(int request) {
        if (ContextCompat.checkSelfPermission(FileImportWechatActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(FileImportWechatActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, request);
        }else{
            Vector<File> allFiles = FileUtils.getAllFiles(FOLDER_WEIXIN, "xlsx", "xls");
            for (File allFile : allFiles) {
                WechatBean bean = new WechatBean();
                String fileName = allFile.getName();
                bean.setFileName(fileName);

                String extensionName = FileUtils.getExtensionName(fileName);
                //todo 根据文件后缀名,选择图标
                if ("xlsx".equals(extensionName)){
                    bean.setDrawable(R.drawable.icon_excel);
                }else if("xls".equals(extensionName)){
                    bean.setDrawable(R.drawable.icon_excel);
                }else if("txt".equals(extensionName)){
                    bean.setDrawable(R.drawable.icon_txt);
                }
                list.add(bean);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    ToastUtils.showShort("" + "权限" + permissions[i] + "申请成功");
                    Vector<File> allFiles = FileUtils.getAllFiles(FOLDER_WEIXIN, "xlsx", "xls");
                    for (File allFile : allFiles) {
                        WechatBean bean = new WechatBean();
                        String fileName = allFile.getName();
                        bean.setFileName(fileName);

                        String extensionName = FileUtils.getExtensionName(fileName);
                        //todo 根据文件后缀名,选择图标
                        if ("xlsx".equals(extensionName)){
                            bean.setDrawable(R.drawable.icon_excel);
                        }else if("xls".equals(extensionName)){
                            bean.setDrawable(R.drawable.icon_excel);
                        }else if("txt".equals(extensionName)){
                            bean.setDrawable(R.drawable.icon_txt);
                        }
                        list.add(bean);
                    }
                }else{
                    ToastUtils.showShort("请给用户授权");
                }
            }
        }

    }


}
